﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GG.Business;

namespace GG.Teacher
{
    public class VektorSpadu
    {
        private static List<int> GetFirstRes(Task task)
        {
            var res = new List<int>();

            for (int i = 0; i < task.VariablesCount; i++)
            {
                res.Add(0);
            }

            return res;
        }

        private static List<List<int>> GetSiblingPoints(List<int> xh, int r)
        {
            var res = new List<List<int>>();

            for (int x = 1; x <= r; x++)
            {
                for (int y = 1; y <= r; y++)
                {
                    res.Add(new List<int>() { xh[0] + x, xh[1] + y });
                    res.Add(new List<int>() { xh[0] + x, xh[1] - y });
                    res.Add(new List<int>() { xh[0] - x, xh[1] + y });
                    res.Add(new List<int>() { xh[0] - x, xh[1] - y });
                }
            }

            return res;
        }

        private static bool IsRestrictionOk(TaskRestriction restriction, List<int> point)
        {
            int leftPartValue = 0;
            for (int i = 0; i < restriction.Coefficients.Count; i++)
            {
                leftPartValue += (int)restriction.Coefficients[i] * point[i];
            }

            if (restriction.ComparisonSign < 0)
            {
                return leftPartValue <= (int)restriction.C;
            }
            else
            {
                if (restriction.ComparisonSign == 0)
                {
                    return leftPartValue == (int)restriction.C;
                }
                else
                    return leftPartValue >= (int)restriction.C;
            }
        }

        private static int GetDelta(List<int> x, List<int> xk, Task task)
        {
            int res = 0;
            for (int i = 0; i < task.VariablesCount; i++)
            {
                res += (int)task.GoalFunctionCoefficients[i] * (xk[i] - x[i]);
            }
            return res;
        }

        private static List<int> GetNextPoint(List<int> xh, List<List<int>> siblingPoints, Task task)
        {
            foreach (var testPoint in siblingPoints)
            {
                if (testPoint.Any(i => i < 0))
                    continue;

                bool okForRestriction = true;
                foreach (var restriction in task.Restrictions)
                {
                    if (!IsRestrictionOk(restriction, testPoint))
                    {
                        okForRestriction = false;
                        break;
                    }
                }
                if (!okForRestriction)
                    continue;

                if (GetDelta(xh, testPoint, task) < 0)
                    return testPoint;
            }
            return null;
        }

        public static void Execute(Task task)
        {
            var x = new List<List<int>>();
            x.Add(GetFirstRes(task));
            int r = 1;
            do
            {
                bool nextPointExist = false;
                do
                {
                    nextPointExist = false;

                    var siblingPoints = GetSiblingPoints(x.Last(), r);

                    var nextPoint = GetNextPoint(x.Last(), siblingPoints, task);

                    if (nextPoint != null)
                    {
                        x.Add(nextPoint);
                        nextPointExist = true;
                    }
                } while (nextPointExist);

                r++;

            } while (r <= task.Rmax);

        }
    }
}
